package cn.edu.sdyu.ie.crs.api;

import cn.edu.sdyu.ie.crs.common.response.PageVO;
import cn.edu.sdyu.ie.crs.common.response.Result;
import cn.edu.sdyu.ie.crs.domain.User;
import cn.edu.sdyu.ie.crs.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理", description = "用户信息管理接口")
@Slf4j
@RestController
@RequestMapping("api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加新用户信息
     * @param user
     * @return
     */
    @Operation(tags = "用户管理", summary = "添加用户信息")
    @PostMapping("")
    public Result<Void> addUser(User user) {

        if(user.getId() != null && !user.getId().equals(0)) {
            log.warn("请求ID参数，若更新对象，请使用PUT方式发起请求。当前将创建新对象");
            user.setId(Integer.valueOf(0));
        }
        userService.saveUser(user);
        return Result.success();
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Operation(tags = "用户管理", summary = "修改用户信息")
    @PutMapping("")
    public Result<Object> updateUser(User user) {

        if(user.getId() == null || user.getId().equals(0)) {
            log.warn("未请求ID参数，使用POST方式创建对象。当前不会更新任何对象");
            return Result.failure();
        }
        userService.saveUser(user);
        return Result.success();
    }

    /**
     * 删除指定ID的用户信息
     * @param id
     * @return
     */
    @Operation(tags = "用户管理", summary = "删除用户信息")
    @DeleteMapping("/{id}")
    public Result<Void> deleteUserById(@PathVariable Integer id) {
        userService.deleteUserById(id);
        return Result.success();
    }

    /**
     * 查询指定ID的用户信息
     * @param id
     * @return
     */
    @Operation(tags = "用户管理", summary = "查询指定ID的用户信息")
//    @Parameters(value = {
//            @Parameter(description = "用户ID")
//    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID")
    })
    @GetMapping("/{id}")
    public Result<User> findUserById(@PathVariable Integer id) {
        User user = userService.findUserById(id);

        if(user == null) {
            return Result.failure();
        }

        return Result.success(user);
    }

    /**
     * 分页查询用户信息
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(tags = "用户管理", summary = "分页查询用户信息")
    @GetMapping("")
    public Result<PageVO<User>> listUsersPage(@RequestParam(defaultValue = "1") Integer page,
                                                        @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<User> list = userService.findByPage(page - 1, pageSize);
        PageVO<User> pageVO = PageVO.getPageVO(list);
        return Result.success(pageVO);
    }
    /**
     * 统计总用户数
     */
    @Operation(tags = "用户管理", summary = "统计总用户数")
    @GetMapping("count/all")
    public Result<Long> countUser() {
        Long count = userService.countUser();
        return Result.success(count);
    }

    /**
     * 根据用户状态统计用户数
     * @param userStatus
     * @return
     */
    @Operation(tags = "用户管理", summary = "根据用户状态统计用户数")
    @GetMapping("count/usageStatus")
    public Result<Long> countUserByUserStatus(@RequestParam Integer userStatus) {
        Long count = userService.countUserByUserStatus(userStatus);
        return Result.success(count);
    }



}
